/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import mx.containers.Canvas;
	import mx.core.Container;
	import mx.events.FlexEvent;
	import mx.events.IndexChangedEvent;
	import mx.events.ScrollEvent;
	
	/**
	 * html面板 ，所有html类型的父类
	 * 
	 */	
	public class EDIHtmlPlan extends Canvas
	{
		private var containerDict:Object = null;
        private var settingDict:Object = null;   
        protected var frameId:String;
		
		/**
		 * 构造 htmlPlan并注册自己到application对象上去
		 * 
		 */		
		public function EDIHtmlPlan() 
		{
			super();
			setStyle("borderStyle","solid");
			setStyle("borderThickness",1);
			setStyle("backgroundAlpha","0.1");
			this.addEventListener(FlexEvent.CREATION_COMPLETE,creationCompleteHandler);
		}		
		
		protected function creationCompleteHandler(event:FlexEvent):void
		{
			if(this.parentApplication!=null)
			{
				this.parentApplication.addEventListener(ScrollEvent.SCROLL,applicationScroolHandler);
			}
		}
		
		private function applicationScroolHandler(event:ScrollEvent):void
		{
			switch(event.direction)
			{
				case "vertical":
				{
					y-=event.delta
					break;
				}
				case "horizontal":
				{
					x-=event.delta
					break;
				}
			}
			invalidateDisplayList();
		}
		
        protected function registerSelf():void
        {
        	EDIApplication.registerHTMLPlan(this);
   			var parentPlan:DisplayObjectContainer=this.parent;
   			EDIApplication.outputDebug("parentPlan: "+parentPlan);
   			while(this.parentApplication!=parentPlan && parentPlan!=null)
   			{
   				if(parentPlan is EDICanvas)
	   			{
	   				(parentPlan as EDICanvas).registerHTMLPlan(this);
	   			}
   				parentPlan=parentPlan.parent;
   			}
        }
        
          /**
        * Build list of container object on the display list path all the way down
        * to this object. We will seed the container classed we find with an event
        * listener will be used to test if this object is to be displayed or not.
        *
        */
        private function buildContainerList():void
        {
            // We are going to store containers against index of child which leads down
            // to IFrame item.
            containerDict = new Dictionary();
            settingDict = new Dictionary();

            var current:DisplayObjectContainer = parent;
            var previous:DisplayObjectContainer = this;
            
            while (current!=null)
            {
                if (current is Container)
                {
                    if (current.contains(previous))
                    {
                        var childIndex:Number = current.getChildIndex(previous);                
                        
                        // Store child index against container
                        containerDict[current] = childIndex;
                        settingDict[current] = childIndex;
                        
                        // Tag on a change listener             
                        current.addEventListener(IndexChangedEvent.CHANGE, handleChange);
                        
                    }
                    
                }        
                
                previous = current;
                current = current.parent;
            }
            
        }

    
        override public function set visible(value:Boolean):void
        {
        	if(visible==value && value==false)
        	{
        		EDIApplication.spliceHtmlPlanArr(this);
        	}
        	super.visible=value;
        }
        
        private function handleChange(event:Event):void
        {
            var target:Object = event.target;
            
            if (event is IndexChangedEvent)
            {
                var changedEvent:IndexChangedEvent = IndexChangedEvent(event)

                var newIndex:Number = changedEvent.newIndex;
                
                visible = checkDisplay(target, newIndex);
                
            }
        }
        
        /**
        * This function updates the selected view child of the signalling container
        * and then compares the path from our IFrame up the displaylist to see if
        * the index settings match. Only an exact match all the way down to our
        * IFrame will satisfy the condition to display the IFrame contents.
        *
        * @param target Object event source
        * @param newIndex Number index from target object.
        * 
        */
        private function checkDisplay(target:Object, newIndex:Number):Boolean
        {
            var valid:Boolean = false;
            
            if (target is Container)
            {
                var container:DisplayObjectContainer = DisplayObjectContainer(target);
                
                // Update current setting
                settingDict[container] = newIndex;
                
                valid = true;
                
                for (var item:Object in containerDict)
                {
                    var index:Number = lookupIndex(item as Container);
                    var setting:Number = lookupSetting(item as Container);
                    
                    valid = valid&&(index==setting);
                }
                
            }
            
            return valid;
        }
		
        /**
        * Return index of child item on path down to this object. If not
        * found then return -1;
        *
        * @param target Container object
        * 
        */
        public function lookupIndex(target:Container):Number
        {
            var index:Number = -1;
            
            try
            {
                index = containerDict[target];
            }
            catch (e:Error)
            {
                // Error not found, we have to catch this or a silent exception
                // will be thrown.
                trace(e);
            }
            
            return index;
        }

        /**
        * Return index of child item on path down to this object. If not
        * found then return -1;
        *
        * @param target Container object
        * 
        */
        public function lookupSetting(target:Container):Number
        {
            var index:Number = -1;
            
            try
            {
                index = settingDict[target];
            }
            catch (e:Error)
            {
                // Error not found, we have to catch this or a silent exception
                // will be thrown.
                trace(e);
            }
            
            return index;
        }   
	}
}